package com.queen.testretrofitrxjava.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.queen.testretrofitrxjava.R;
import com.queen.testretrofitrxjava.presenter.presenterView.BasePresenterView;
import com.queen.testretrofitrxjava.ui.adapter.OnItemClickListener;
import com.queen.testretrofitrxjava.ui.adapter.TopMovieAdapter;
import com.queen.testretrofitrxjava.applications.ApplicationController;
import com.queen.testretrofitrxjava.entity.Subject;
import com.queen.testretrofitrxjava.internal.di.components.DaggerTopMovieComponent;
import com.queen.testretrofitrxjava.presenter.TopMoviePresenter;
import com.queen.testretrofitrxjava.ui.views.DividerItemDecoration;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liukun on 16/3/12.
 */
public class TopMovieFragment extends Fragment implements BasePresenterView<List<Subject>> {

    public static final String TAG = TopMovieFragment.class.getSimpleName();

    @Bind(R.id.fragment_top_movie_RV)
    RecyclerView fragmentTopMovieRV;

    @Inject
    TopMovieAdapter adapter;

    @Inject
    TopMoviePresenter topMoviePresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //依赖注入
        DaggerTopMovieComponent.builder()
                .applicationComponent(ApplicationController.getsInstance().getApplicationComponent())
                .build()
                .inject(this);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_top_movie, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        initRecyclerView();

        topMoviePresenter.setView(this);

        if (savedInstanceState == null) {
            topMoviePresenter.initialize();
        }
    }

    private void loadData(){
        topMoviePresenter.getTopMovie();
    }

    //TopMovieView接口方法
    @Override
    public void renderView(List<Subject> subjectList) {
        if (subjectList != null) {
            adapter.setMovieArray(subjectList);
        }
    }

    private void initRecyclerView(){
        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentTopMovieRV.setLayoutManager(mLayoutManager);

        fragmentTopMovieRV.addOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastPosition = mLayoutManager.findLastVisibleItemPosition();
                int totalCount = mLayoutManager.getItemCount();

                /**
                 * lastPosition >= totalCount -3   如果当前页的最后一个条目到了总条目数的倒数第二个
                 * dy > 0                          如果当前是向上滑动
                 * index < movieTotalCount         还有数据可加载
                 * !isLoading                      当前并没有在请求数据
                 */
//                if (lastPosition >= totalCount -3 && dy > 0 && index < movieTotalCount && !isLoading) {
//                    loadData();
//                }

                if (lastPosition >= totalCount -3 && dy > 0) {
                    loadData();
                }

            }
        });


        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Subject s = topMoviePresenter.getMovieByPosition(position);
                Fragment target = MovieDetailFragment.getInstance(s.getId());

                getFragmentManager()
                        .beginTransaction()
                        .replace(R.id.activity_main_RL, target)
                        .addToBackStack(TAG)
                        .commit();
            }
        });
        fragmentTopMovieRV.setAdapter(adapter);
        fragmentTopMovieRV.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }



}
